import { Injectable, Logger } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { ConfigService } from '@nestjs/config';
import { BaseService } from '@wwjCommon/base/base.service';
import { CacheService } from '@wwjCommon/cache/cache.service';
import { LoggingService } from '@wwjCommon/logging/logging.service';
import { UploadService } from '@wwjVendor/upload/upload.service';
import { PayService } from '@wwjVendor/pay/pay.service';
import { SmsService } from '@wwjVendor/sms/sms.service';
import { NoticeService } from '@wwjVendor/notice/notice.service';

@Injectable()
export class CorePosterService extends BaseService<any> {
  private readonly logger = new Logger(CorePosterService.name);

  constructor(
    @InjectRepository(Object)
    protected readonly repository: Repository<any>,
    private readonly cacheService: CacheService,
    private readonly configService: ConfigService,
    private readonly loggingService: LoggingService,
    private readonly uploadService: UploadService,
    private readonly payService: PayService,
    private readonly smsService: SmsService,
    private readonly noticeService: NoticeService,
  ) {
    super(repository);
  }

  /**
   * getDefaultPosterIdByType
   * 对应 PHP: CorePosterService_core::getDefaultPosterIdByType()
   * 逻辑类型: undefined - undefined
   */
  async getDefaultPosterIdByType(site_id: any, type: any, addon: any) {
    // 基于PHP真实逻辑: getDefaultPosterIdByType
    // PHP原文: return (new Poster())->where([             'site_id' => $site_id,             'type' => $type,             'is_default' => 1,             'status' => ...
return this.posterService.where([
            site_id: site_id,
            type: type,
            is_default: 1,
            status: 1,
            addon: addon,
        )).value('id') ?: 0;
    }

    /**
     * 创建模板
     * @param site_id
     * @param addon
     * @param data
     * @return true
     */
    async add(site_id, addon, data)
    {
        data.addon = addon;
        data.site_id = site_id;
        this.posterService.create(data);
        return true;
    }


    /**
     * 删除
     * @param condition
     * @return true
     */
    async del(condition)
    {
        this.posterService.where(condition).delete(];
        return true;
    }

    /**
     * 海报类型
     * @param string type
     * @return */
    async getType(type = '')
    {
        return PosterDict.getType(type);
    }

    /**
     * 海报模板
     * @param addon
     * @param type
     * @return array|null
     */
    async getTemplateList(addon = '', type = '')
    {
        const addon_load = new DictLoader('Poster');
        return addon_load.load([
            addon: addon,
            type: type
        )];
    }

    /**
     * 实例化模板引擎
     * @param site_id
     * @param string poster_type
     * @return PosterLoader
     */
    async driver(site_id, poster_type = 'poster')
    {
        return new PosterLoader(poster_type, [)];
    }

    /**
     * 获取海报
     * @param int site_id
     * @param string|int id 海报id
     * @param string|int type 模板类型
     * @param param
     * @param string channel
     * @param bool is_throw_exception
     * @return string|null
     */
    async get(int site_id, id, type, param = [), channel = '', is_throw_exception = true)
    {
        const condition = [
            ['site_id', '=', site_id],
            ['type', '=', type],
            ['status', '=', PosterDict.ON],
        ];
        if (!!id)) {
            // 查询指定海报
            condition[] = ['id', '=', id];
  }

  /**
   * add
   * 对应 PHP: CorePosterService_core::add()
   * 逻辑类型: undefined - undefined
   */
  async add(site_id: any, addon: any, data: any) {
    // 基于PHP真实逻辑: add
    // PHP原文: $data['addon'] = $addon;         $data['site_id'] = $site_id;         (new Poster())->create($data);         return true;     }       /**      * 删除   ...
data.addon = addon;
        data.site_id = site_id;
        this.posterService.create(data);
        return true;
    }


    /**
     * 删除
     * @param condition
     * @return true
     */
    async del(condition)
    {
        this.posterService.where(condition).delete(];
        return true;
    }

    /**
     * 海报类型
     * @param string type
     * @return */
    async getType(type = '')
    {
        return PosterDict.getType(type);
    }

    /**
     * 海报模板
     * @param addon
     * @param type
     * @return array|null
     */
    async getTemplateList(addon = '', type = '')
    {
        const addon_load = new DictLoader('Poster');
        return addon_load.load([
            addon: addon,
            type: type
        )];
    }

    /**
     * 实例化模板引擎
     * @param site_id
     * @param string poster_type
     * @return PosterLoader
     */
    async driver(site_id, poster_type = 'poster')
    {
        return new PosterLoader(poster_type, [)];
    }

    /**
     * 获取海报
     * @param int site_id
     * @param string|int id 海报id
     * @param string|int type 模板类型
     * @param param
     * @param string channel
     * @param bool is_throw_exception
     * @return string|null
     */
    async get(int site_id, id, type, param = [), channel = '', is_throw_exception = true)
    {
        const condition = [
            ['site_id', '=', site_id],
            ['type', '=', type],
            ['status', '=', PosterDict.ON],
        ];
        if (!!id)) {
            // 查询指定海报
            condition[] = ['id', '=', id];
  }

  /**
   * del
   * 对应 PHP: CorePosterService_core::del()
   * 逻辑类型: undefined - undefined
   */
  async del(condition: any) {
    // 基于PHP真实逻辑: del
    // PHP原文: (new Poster())->where($condition)->delete();         return true;     }      /**      * 海报类型      * @param string $type      * @return array      */  ...
this.posterService.where(condition).delete(];
        return true;
    }

    /**
     * 海报类型
     * @param string type
     * @return */
    async getType(type = '')
    {
        return PosterDict.getType(type);
    }

    /**
     * 海报模板
     * @param addon
     * @param type
     * @return array|null
     */
    async getTemplateList(addon = '', type = '')
    {
        const addon_load = new DictLoader('Poster');
        return addon_load.load([
            addon: addon,
            type: type
        )];
    }

    /**
     * 实例化模板引擎
     * @param site_id
     * @param string poster_type
     * @return PosterLoader
     */
    async driver(site_id, poster_type = 'poster')
    {
        return new PosterLoader(poster_type, [)];
    }

    /**
     * 获取海报
     * @param int site_id
     * @param string|int id 海报id
     * @param string|int type 模板类型
     * @param param
     * @param string channel
     * @param bool is_throw_exception
     * @return string|null
     */
    async get(int site_id, id, type, param = [), channel = '', is_throw_exception = true)
    {
        const condition = [
            ['site_id', '=', site_id],
            ['type', '=', type],
            ['status', '=', PosterDict.ON],
        ];
        if (!!id)) {
            // 查询指定海报
            condition[] = ['id', '=', id];
  }

  /**
   * getType
   * 对应 PHP: CorePosterService_core::getType()
   * 逻辑类型: undefined - undefined
   */
  async getType(type: any) {
    // 基于PHP真实逻辑: getType
    // PHP原文: return PosterDict::getType($type);     }      /**      * 海报模板      * @param $addon      * @param $type      * @return array|null      */     public fu...
return PosterDict.getType(type);
    }

    /**
     * 海报模板
     * @param addon
     * @param type
     * @return array|null
     */
    async getTemplateList(addon = '', type = '')
    {
        const addon_load = new DictLoader('Poster');
        return addon_load.load([
            addon: addon,
            type: type
        )];
    }

    /**
     * 实例化模板引擎
     * @param site_id
     * @param string poster_type
     * @return PosterLoader
     */
    async driver(site_id, poster_type = 'poster')
    {
        return new PosterLoader(poster_type, [)];
    }

    /**
     * 获取海报
     * @param int site_id
     * @param string|int id 海报id
     * @param string|int type 模板类型
     * @param param
     * @param string channel
     * @param bool is_throw_exception
     * @return string|null
     */
    async get(int site_id, id, type, param = [), channel = '', is_throw_exception = true)
    {
        const condition = [
            ['site_id', '=', site_id],
            ['type', '=', type],
            ['status', '=', PosterDict.ON],
        ];
        if (!!id)) {
            // 查询指定海报
            condition[] = ['id', '=', id];
  }

  /**
   * getTemplateList
   * 对应 PHP: CorePosterService_core::getTemplateList()
   * 逻辑类型: undefined - undefined
   */
  async getTemplateList(addon: any, type: any) {
    // 基于PHP真实逻辑: getTemplateList
    // PHP原文: $addon_load = new DictLoader('Poster');         return $addon_load->load([             'addon' => $addon,             'type' => $type         ]);     ...
const addon_load = new DictLoader('Poster');
        return addon_load.load([
            addon: addon,
            type: type
        )];
    }

    /**
     * 实例化模板引擎
     * @param site_id
     * @param string poster_type
     * @return PosterLoader
     */
    async driver(site_id, poster_type = 'poster')
    {
        return new PosterLoader(poster_type, [)];
    }

    /**
     * 获取海报
     * @param int site_id
     * @param string|int id 海报id
     * @param string|int type 模板类型
     * @param param
     * @param string channel
     * @param bool is_throw_exception
     * @return string|null
     */
    async get(int site_id, id, type, param = [), channel = '', is_throw_exception = true)
    {
        const condition = [
            ['site_id', '=', site_id],
            ['type', '=', type],
            ['status', '=', PosterDict.ON],
        ];
        if (!!id)) {
            // 查询指定海报
            condition[] = ['id', '=', id];
  }

  /**
   * driver
   * 对应 PHP: CorePosterService_core::driver()
   * 逻辑类型: undefined - undefined
   */
  async driver(site_id: any, poster_type: any) {
    // 基于PHP真实逻辑: driver
    // PHP原文: return new PosterLoader($poster_type, []);     }      /**      * 获取海报      * @param int $site_id      * @param string|int $id 海报id      * @param strin...
return new PosterLoader(poster_type, [)];
    }

    /**
     * 获取海报
     * @param int site_id
     * @param string|int id 海报id
     * @param string|int type 模板类型
     * @param param
     * @param string channel
     * @param bool is_throw_exception
     * @return string|null
     */
    async get(int site_id, id, type, param = [), channel = '', is_throw_exception = true)
    {
        const condition = [
            ['site_id', '=', site_id],
            ['type', '=', type],
            ['status', '=', PosterDict.ON],
        ];
        if (!!id)) {
            // 查询指定海报
            condition[] = ['id', '=', id];
  }

  /**
   * get
   * 对应 PHP: CorePosterService_core::get()
   * 逻辑类型: undefined - undefined
   */
  async get(site_id: any[], id: any[], type: any[], param: any[], channel: any[], is_throw_exception: any[]) {
    // 基于PHP真实逻辑: get
    // PHP原文: $condition = [             ['site_id', '=', $site_id],             ['type', '=', $type],             ['status', '=', PosterDict::ON],         ];      ...
const condition = [
            ['site_id', '=', site_id],
            ['type', '=', type],
            ['status', '=', PosterDict.ON],
        ];
        if (!!id)) {
            // 查询指定海报
            condition[] = ['id', '=', id];
  }

  /**
   * createRecord (原方法名: create)
   * 对应 PHP: CorePosterService_core::create()
   * 逻辑类型: undefined - undefined
   * 注意: 为避免与BaseService方法冲突，已重命名
   */
  async createRecord(site_id: any[], poster: any[], data: any[], dir: any[], file_path: any[], channel: any[]) {
    // 基于PHP真实逻辑: create
    // PHP原文: //将模版中的部分待填充值替换         $core_upload_service = new CoreFetchService();         if ($poster['global']['bgType'] == 'url') {             if (!empty($pos...
//将模版中的部分待填充值替换
        const core_upload_service = this.coreFetchService;
        if (poster.global.bgType == 'url') {
            if (!!poster.global.bgUrl) && str_contains(poster.global.bgUrl, 'http://') || str_contains(poster.global.bgUrl, 'https://')) {
                //判断是否是是远程图片,远程图片需要本地化
                const temp_dir = 'file/'  + 'image'  + '/' . site_id  + '/' . date('Ym')  + '/' . date('d');
                try {
                    poster.global.bgUrl = core_upload_service.image(poster.global.bgUrl, site_id, temp_dir, FileDict.LOCAL).url || '';
                } catch (\BusinessException e) {
    // 待实现
  }
            }
  }
}
